package com.mcxx.modules.miniSalvation.salary.service.write;

import cn.hutool.core.util.ObjectUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.salary.dao.write.MemberSalaryWriteDao;
import com.mcxx.modules.miniSalvation.salary.entity.FamilySalaryEntity;
import com.mcxx.modules.miniSalvation.salary.entity.MemberSalaryEntity;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryReadService;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;


@Service
@TaTransactional
public class FamilyMemberSalaryWriteService extends BaseService {
    @Autowired
    private MemberSalaryWriteDao memberSalaryWriteDao;
    @Autowired
    private FamilySalaryReadService familySalaryReadService;
    @Autowired
    private FamilySalaryWriteService familySalaryWriteService;


    public int insertIgnoreNull(MemberSalaryEntity newMemberSalarys){
        return memberSalaryWriteDao.insertIgnoreNull(newMemberSalarys);
    }


    public int insertBachSalary(List<MemberSalaryEntity> newMemberSalarys){
        return memberSalaryWriteDao.insertBachSalary(newMemberSalarys);
    }


    public int batchDeleteByIds(List<Map<String, String>> memberSalaryIds) {
        return memberSalaryWriteDao.batchDeleteByIds(memberSalaryIds);
    }


    public void copySalaryByOrderId(String orderId, String newOrderId, String curMonth) {
        int rows = memberSalaryWriteDao.copySalaryByOrderId(orderId, newOrderId, curMonth);
        if (rows < 1) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void batchChangeExpireDate(Map<String, Object> map) {
        memberSalaryWriteDao.batchChangeExpireDate(map);
    }


    public void modifySalary(List<MemberSalaryEntity> memberSaList, String orderId,String stopMonth){
        FamilySalaryEntity familySaEntity = familySalaryReadService.getFamilySumSalaryByOrderId(orderId);
        if(ObjectUtil.isNull(familySaEntity)){
            throw new BizException(FailureCode.ERR_1000023);
        }

        //familySaEntity.setEndDate(DateUtil.reduceMonthb(familySaEntity.getStartDate(),1));

        this.modifySalaryMember(familySaEntity, memberSaList, stopMonth);

    }

    public int modifySalaryMember(FamilySalaryEntity oldFamilySalary, List<MemberSalaryEntity> newMemberSalarys,
                                  String stopMonth) {

//        familySalaryEntity.setId(oldFamilySalary.getId());
//        familySalaryEntity.setEndDate(DateUtil.reduceMonthDate(stopMonth,1));
//        familySalaryEntity.setUpdateTime(Date.valueOf(LocalDate.now()));



        familySalaryWriteService.updateEndDate(oldFamilySalary.getOrderId(), DateUtil.reduceMonth(stopMonth,1));
        for (MemberSalaryEntity memberSalaryEntity : newMemberSalarys) {
            MemberSalaryEntity updateEntity=new MemberSalaryEntity();
            updateEntity.setId(memberSalaryEntity.getId());
            updateEntity.setEndDate(DateUtil.reduceMonthDate(stopMonth,1));
            memberSalaryWriteDao.updateIgnoreNull(updateEntity);
        }
        return 0;
    }

    public int reSalaryMember(FamilySalaryEntity oldFamilySalary, List<MemberSalaryEntity> newMemberSalarys,
                              String startMonth, String stopMonth, String orderId, Boolean change) {

        oldFamilySalary.setId(Constant.getUUID());
        oldFamilySalary.setOrderId(orderId);
        try {
            oldFamilySalary.setStartDate(DateUtil.sdf6.parse(startMonth));
            oldFamilySalary.setEndDate(DateUtil.sdf6.parse(stopMonth));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new BizException("-1","系统参数异常，请联系管理员处理");
        }

        oldFamilySalary.setInputDate(DateUtil.getMonthString());
        oldFamilySalary.setSaveReason("暂停恢复");
        if(change){
            oldFamilySalary.setSaveReason("暂停补发");
            oldFamilySalary.setSalaryType(Constant.SalaryType.SUSPEND_REISSUE);
        }
        familySalaryWriteService.insertIgnoreNull(oldFamilySalary);
        for (MemberSalaryEntity memberSalaryEntity : newMemberSalarys) {
            try {
                memberSalaryEntity.setStartDate(DateUtil.sdf6.parse(startMonth));
                memberSalaryEntity.setEndDate(DateUtil.sdf6.parse(stopMonth));
            } catch (ParseException e) {
                e.printStackTrace();
                throw new BizException("-1","系统参数异常，请联系管理员处理");
            }

            memberSalaryEntity.setOrderId(orderId);
            memberSalaryEntity.setId(Constant.getUUID());
            memberSalaryEntity.setInputDate(DateUtil.getMonthString());
            memberSalaryEntity.setSaveReason("暂停恢复");
            if(change){
                memberSalaryEntity.setSaveReason("暂停补发");
                memberSalaryEntity.setSalaryType(Constant.SalaryType.SUSPEND_REISSUE);
            }
        }
        if(CollectionUtils.isEmpty(newMemberSalarys)) {
            return 0;
        }
        return memberSalaryWriteDao.insertBachSalary(newMemberSalarys);
    }

}
